<template>
  <el-dialog
    :title="!dataForm.id ? '申请' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="请假日期" prop="leaveDate">
      <el-col :span="11">
        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.leaveDate" style="width: 100%;"></el-date-picker>
      </el-col>
    </el-form-item>
    <el-form-item label="员工编号" prop="empId">
      <el-input v-model="dataForm.empId" placeholder="员工编号"></el-input>
    </el-form-item>
    <el-form-item label="请假编号" prop="leaveId">
      <el-input v-model="dataForm.leaveId" placeholder="请假编号"></el-input>
    </el-form-item>
    <el-form-item label="请假开始时间" prop="startDate">
      <el-col :span="11">
        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.startDate" style="width: 100%;"></el-date-picker>
      </el-col>
    </el-form-item>
    <el-form-item label="请假结束时间" prop="endDate">
    <el-col :span="11">
        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.endDate" style="width: 100%;"></el-date-picker>
      </el-col>
    </el-form-item>
    <el-form-item label="请假天数" prop="leaveCount">
      <el-input v-model="dataForm.leaveCount" placeholder="请假天数"></el-input>
    </el-form-item>
    <el-form-item label="请假理由描述" prop="leaveReason">
      <el-input v-model="dataForm.leaveReason" placeholder="请假理由描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          leaveDate: '',
          empId: '',
          leaveId: '',
          startDate: '',
          endDate: '',
          leaveCount: '',
          leaveReason: '',
          mark: '',
          markName: ''
        },
        dataRule: {
          leaveDate: [
            { required: true, message: '请假日期不能为空', trigger: 'blur' }
          ],
          empId: [
            { required: true, message: '员工编号不能为空', trigger: 'blur' }
          ],
          leaveId: [
            { required: true, message: '请假编号不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '请假开始日期不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '请假结束时间不能为空', trigger: 'blur' }
          ],
          leaveCount: [
            { required: true, message: '请假天数不能为空', trigger: 'blur' }
          ],
          leaveReason: [
            { required: true, message: '请假理由描述不能为空', trigger: 'blur' }
          ],
          mark: [
            { required: true, message: '确认标志不能为空', trigger: 'blur' }
          ],
          markName: [
            { required: true, message: '确认人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/employee/leaveinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.leaveDate = data.leaveInfo.leaveDate
                this.dataForm.empId = data.leaveInfo.empId
                this.dataForm.leaveId = data.leaveInfo.leaveId
                this.dataForm.startDate = data.leaveInfo.startDate
                this.dataForm.endDate = data.leaveInfo.endDate
                this.dataForm.leaveCount = data.leaveInfo.leaveCount
                this.dataForm.leaveReason = data.leaveInfo.leaveReason
                this.dataForm.mark = data.leaveInfo.mark
                this.dataForm.markName = data.leaveInfo.markName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/employee/leaveinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'leaveDate': this.dataForm.leaveDate,
                'empId': this.dataForm.empId,
                'leaveId': this.dataForm.leaveId,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'leaveCount': this.dataForm.leaveCount,
                'leaveReason': this.dataForm.leaveReason,
                'mark': this.dataForm.mark,
                'markName': this.dataForm.markName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
